package com.mall.mallproduct.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.mall.mallproduct.entity.AttrEntity;
import com.mall.mallproduct.service.AttrService;
import com.mall.mallproduct.service.CategoryService;
import com.mall.mallproduct.vo.AttrGroupRelationVO;
import com.mall.mallproduct.vo.AttrGroupWithAttrsVO;
import com.mall.mallproduct.vo.AttrRespVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.mall.mallproduct.entity.AttrGroupEntity;
import com.mall.mallproduct.service.AttrGroupService;
import com.common.utils.PageUtils;
import com.common.utils.R;



/**
 * 属性分组
 *
 * @author zxc
 * @email zxc@gmail.com
 * @date 2020-08-13 17:55:40
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    CategoryService categoryService;

    @Autowired
    AttrService attrService;


    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupWithAttrsVO> vos=attrGroupService.getAttrGroupWithAttrsBycatelogId(catelogId);

        return  R.ok().put("data",vos);
    }
    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
   // @RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId){
     //   PageUtils page = attrGroupService.queryPage(params);
       PageUtils page=attrGroupService.queryPage(params,catelogId);
        return R.ok().put("page", page);
    }

    /**
     *
     * 查询关联信息
     */
    @GetMapping("/{attrGroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrGroupId") Long attrGroupId){
        List<AttrEntity> entities=attrService.getAttrRelation(attrGroupId);
        return R.ok().put("data",entities);
    }

    /**
     *
     * 查询没有关联的信息
     */
    @GetMapping("/{attrGroupId}/noattr/relation")
    public R noattrRelation(@PathVariable("attrGroupId") Long attrGroupId,
                            @RequestParam Map<String, Object> params){
        PageUtils pageUtils=attrService.getnoAttrRelation(params,attrGroupId);
        return R.ok().put("page",pageUtils);
    }

    /**
     * 移除关联信息
     */

    @PostMapping("/attr/relation/delete")
    public  R delRelation(@RequestBody List<AttrGroupRelationVO> relationVO){
      attrService.delRelation(relationVO);
        return  R.ok();
    }

    /**
     *保存关联信息
     */
    @PostMapping("/attr/relation")
    public  R relation(@RequestBody  List<AttrGroupRelationVO> relationVO){
        attrGroupService.saveRelation(relationVO);
        return  R.ok();
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
   // @RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        Long [] path=categoryService.findCategoryPath(catelogId);
        attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
  //  @RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
   // @RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
  //  @RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
